package com.jingyanzi.sys.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingyanzi.sys.dao.SysFeedbackMapper;
import com.jingyanzi.sys.domain.FeedbackDO;
import com.jingyanzi.sys.domain.query.FeedbackQueryDO;
import com.jingyanzi.sys.domain.result.PageAll;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.IFeedbackService;
import com.jingyanzi.sys.entity.SysFeedback;
import com.jingyanzi.sys.entity.SysFeedbackExample;
import com.jingyanzi.sys.entity.SysFeedbackExample.Criteria;
import com.jingyanzi.sys.util.BeanUtilsExtends;

@Service("iFeedbackService")
public class FeedbackServiceImpl extends BaseServiceImpl implements IFeedbackService {
	
	@Autowired
	private SysFeedbackMapper feedbackMapper;

	@Override
	public ResultDO add(FeedbackDO feedbackDO) {
		ResultDO result=new ResultSupport(true);
		if(feedbackDO==null) {
			result.setSuccess(false);
			return result;
		}
		try {
			
			SysFeedback feedback=new SysFeedback();
			BeanUtilsExtends.copyProperties(feedback, feedbackDO);
			feedback.setIsDelete(1);
			feedback.setCreatetime(new Date());
			int insert = feedbackMapper.insert(feedback);
			if(insert==0) {
				result.setSuccess(false);
			}
		}catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(false);
		}
		
		// TODO Auto-generated method stub
		return result;
	}

	@Override
	public ResultDO remove(Integer id) {
	
		ResultDO result=new ResultSupport(true);
		if(id==null) {
			result.setSuccess(false);
			return result;
		}
		SysFeedback selectByPrimaryKey = feedbackMapper.selectByPrimaryKey(id);
		selectByPrimaryKey.setIsDelete(-1);
		
		try {
			
			int updateByPrimaryKeySelective = feedbackMapper.updateByPrimaryKeySelective(selectByPrimaryKey);
			
			if(updateByPrimaryKeySelective==0) {
				result.setSuccess(true);
			}
		}catch (Exception e) {
			result.setSuccess(false);
		}
		
		return result;
	}

	@Override
	public ResultDO modifi(FeedbackDO feedBackDO) {
		
		ResultDO result=new ResultSupport(true);
		
		if(feedBackDO==null||feedBackDO.getId()==null) {
			result.setSuccess(false);
			result.setErrorMsg("失败");
			return result;
		}
		SysFeedback feedback=new SysFeedback();
		BeanUtilsExtends.copyProperties(feedback, feedBackDO);
		
		
		int updateByPrimaryKeySelective = feedbackMapper.updateByPrimaryKeySelective(feedback);
		
		if(updateByPrimaryKeySelective==0) {
			result.setSuccess(false);
			result.setErrorMsg("修改失败");
			return result;
		}
		result.setSuccess(true);
		
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		
		ResultDO result=new ResultSupport(true);
		if(id<0) {
			result.setSuccess(true);
			result.setErrorMsg("失败");
			return result;
		}
		SysFeedback selectByPrimaryKey = feedbackMapper.selectByPrimaryKey(id);
		if(selectByPrimaryKey==null) {
			result.setSuccess(false);
			return result;
		}
		
		FeedbackDO fee=new FeedbackDO();
		BeanUtilsExtends.copyProperties(fee, selectByPrimaryKey);
		result.setModel(ResultDO.FIRST_MODEL_KEY,fee);
		return result;
	}

	@Override
	public ResultDO query(FeedbackQueryDO queryDO,PageAll page) {
		
		ResultDO result=new ResultSupport(true);
		
		
		SysFeedbackExample  example=new SysFeedbackExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andIsDeleteEqualTo(1);
	
		PageHelper.startPage(page.getPageindex(), page.getPagesize());
		List<SysFeedback> selectByExample = feedbackMapper.selectByExample(example);
		
		PageInfo<SysFeedback> pageInfo=new PageInfo<SysFeedback>(selectByExample);
		
		List<SysFeedback> list = pageInfo.getList();
		page.setTotal((int)pageInfo.getTotal());
		
		
		List<FeedbackDO> lists=new  ArrayList<FeedbackDO>();
		
		
		for(SysFeedback s:list) {
			FeedbackDO f1=new FeedbackDO();
			BeanUtilsExtends.copyProperties(f1, s);
			lists.add(f1);
		}
		result.setModel("page", page);
		result.setModel(ResultDO.FIRST_MODEL_KEY, lists);
		return result;
	}

}
